package com.first.system.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.first.common.pojo.TbHeadNav;
import com.first.common.vo.Result;
import com.first.system.dao.PreDao;
import com.first.system.service.PreService;
import com.first.system.vo.PreVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @program: first-project
 * @description:
 * @author: 闫锦超
 * @create: 2023-05-26 15:02
 **/
@Service
public class PreServiceImpl implements PreService {
    @Autowired
    private PreDao preDao;
    @Override
    public Result getAllHeadNav(Integer currentPage, Integer pageSize, PreVo preVo) {
        Page<TbHeadNav> page = new Page(currentPage,pageSize);
        QueryWrapper<TbHeadNav> wrapper = new QueryWrapper<>();
        //伪删除的不展示
        wrapper.eq("is_delete",0);
        if (StringUtils.hasText(preVo.getHeadName())){
            wrapper.like("head_name",preVo.getHeadName());
        }
        if(preVo.getIsDiable()!=""&&preVo.getIsDiable()!=null){
            wrapper.eq("is_diable",preVo.getIsDiable());
        }
        Page<TbHeadNav> tbHeadNavPage = preDao.selectPage(page, wrapper);
        return Result.success(tbHeadNavPage);
    }

    @Override
    public Result changeStatus(Integer headId, String isDiable) {
        TbHeadNav tbHeadNav = new TbHeadNav();
        tbHeadNav.setHeadId(headId);
        tbHeadNav.setIsDiable(isDiable);
        int i = preDao.updateById(tbHeadNav);
        if(i!=0){
            return Result.success();
        }
        return null;
    }

    @Override
    public Result deleteHeadNav(TbHeadNav tbHeadNav) {
        tbHeadNav.setIsDelete("1");
        int i = preDao.updateById(tbHeadNav);
        if(i!=0){
            return Result.success();
        }
        return null;
    }

    @Override
    public Result addHeadNav(TbHeadNav tbHeadNav) {
        TbHeadNav tbHeadNav1 = new TbHeadNav(null, tbHeadNav.getHeadName(), tbHeadNav.getHref(), "0", "0");
        int insert = preDao.insert(tbHeadNav1);
        if(insert!=0){
            return Result.success();
        }
        return null;
    }

    @Override
    public Result updateHeadNav(TbHeadNav tbHeadNav) {
        int i = preDao.updateById(tbHeadNav);
        if(i!=0){
            return Result.success();
        }
        return null;
    }
}
